import { route, GET, POST } from 'awilix-express';

@route('/sums')
export default class SumsApi {

  constructor({ sumsService }) {
    this.sumsService = sumsService;
  }

  @route('/getSumsAll')
  @POST()
  async getSumsAll(req, res){
    const {
      mark
    } = req.body
    const { success, data: list, message } = await this.sumsService.getSumsAll({mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  @route('/getSumslist')
  @POST()
  async getSumslist(req, res) {
    const { t_name, mark, limit, offset } = req.body
    const { success, data: {count, rows}, message } = await this.sumsService.getSumslist({t_name, mark, limit, offset});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, {count, rows});
  }

  // 总求和（总访问量、售罄访问量、无效访问量、请求IP数量、有效IP请求、无效IP请求）
  @route('/getAllSum')
  @GET()
  async getAllSum(req, res) {
    const { success, data: list, message } = await this.sumsService.getAllSum();
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  // 今日总求和（总访问量、售罄访问量、无效访问量、请求IP数量、有效IP请求、无效IP请求）
  @route('/getDaySum')
  @POST()
  async getDaySum(req, res) {
    const {
      days
    } = req.body;
    const { success, data: list, message } = await this.sumsService.getDaySum({days});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  // 根据条件查询访问统计数据
  @route('/getSearchSum')
  @POST()
  async getSearchSum(req, res) {
    const { t_no } = req.body;
    const { success, data: list, message } = await this.sumsService.getSearchSum({ t_no });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message, list);
  }

  // 批量添加
  @route('/bulkSums')
  @POST()
  async bulkSums(req, res) {
    const { sums } = req.body;
    const { success, message } = await this.sumsService.bulkSums(sums);
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

   // 总请求次数
  @route('/incSum')
  @POST()
  async incSum(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incSum({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 代理IP消耗量
  @route('/incSip')
  @POST()
  async incSip(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incSip({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 无效IP
  @route('/incWip')
  @POST()
  async incWip(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incWip({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 虚拟页面
  @route('/incXny')
  @POST()
  async incXny(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incXny({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

   // 链接关闭
  @route('/incClose')
  @POST()
  async incClose(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incClose({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

   // 查询超时
  @route('/incCtime')
  @POST()
  async incCtime(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incCtime({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 无效查询
  @route('/incWsearch')
  @POST()
  async incWsearch(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incWsearch({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 已售罄
  @route('/incOver')
  @POST()
  async incOver(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incOver({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

   // 查询成功
  @route('/incSearch')
  @POST()
  async incSearch(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incSearch({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 无效登录
  @route('/incWlogo')
  @POST()
  async incWlogo(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incWlogo({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 登录成功
  @route('/incLogo')
  @POST()
  async incLogo(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incLogo({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  // 登录失败
  @route('/incFlogo')
  @POST()
  async incFlogo(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incFlogo({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  // 下单成功
  @route('/incSuccess')
  @POST()
  async incSuccess(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incSuccess({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  // 下单失败
  @route('/incLoser')
  @POST()
  async incLoser(req, res) {
    const {
      t_no,
      t_create,
      mark
    } = req.body;
    const { success, message } = await this.sumsService.incLoser({
      t_no,
      t_create,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/addSums')
  @POST()
  async addSums(req, res) {
    const { 
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
      mark
     } = req.body;
    const { success, message } = await this.sumsService.addSums({
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
      mark
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }

  @route('/delSums')
  @POST()
  async delSums(req, res) {
    const { id, mark } = req.body;
    const { success, message } = await this.sumsService.delSums({id, mark});
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }


  @route('/updateSums')
  @POST()
  async updateSums(req, res) {
    const {
      id,
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
    } = req.body;
    const { success, message } = await this.sumsService.updateSums({
      id,
      t_name,
      t_type,
      t_sumHttps,
      t_virtual,
      t_loadSearchFailed,
      t_loadSearchClosed,
      t_loadLogoFailed,
      t_invalidLogo,
      t_invalidIP,
      t_invalidSearch,
      t_success,
      startTime,
      updateTime,
    });
    if (!success) {
      res.failPrint(message);
      return;
    }
    res.successPrint(message);
  }
}